package com.cnfangmao.masterdata.service.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cnfangmao.masterdata.common.util.MD5Utils;
import com.cnfangmao.masterdata.common.util.SnowFlakeUtils;
import com.cnfangmao.masterdata.dao.AccountDao;
import com.cnfangmao.masterdata.dao.AccountRoleDao;
import com.cnfangmao.masterdata.dto.AccountDto;
import com.cnfangmao.masterdata.dto.AccountSearchDto;
import com.cnfangmao.masterdata.dto.ChangePasswordDto;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.model.Account;
import com.cnfangmao.masterdata.model.AccountRole;
import com.cnfangmao.masterdata.service.AccountService;

/**
 * <p>
 * 主数据中心用户 服务实现类
 * </p>
 *
 * @author chenxd
 * @since 2020-04-14
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountDao, Account> implements AccountService {

	@Autowired
	private AccountDao dao;
	
	@Autowired
	private AccountRoleDao accountRoleDao;

	@Override
	public Account findByUserName(String userName) {
		Account account = null;
			account = dao.findByUserName(userName);
		return account;
	}

	@Override
	public boolean update(AccountDto dto, String modifiedBy) {
		boolean result = false;
			Account account = dao.selectById(dto.getUserId());
			account.setUserName(dto.getUserName());
			account.setPassword(MD5Utils.getMD516(dto.getPassword()));
			account.setUserCode(dto.getUserCode());
			account.setUserImage(dto.getUserImage());
			account.setModifiedOn(new Date());
			account.setModifiedBy(modifiedBy);
			result = dao.updateById(account) > 0;
			if(result){
				accountRoleDao.deleteAccountRoleByUserId(account.getUserId());
				AccountRole accountRole =new AccountRole();
				accountRole.setUserRoleId(SnowFlakeUtils.generator());
				accountRole.setUserId(account.getUserId());
				accountRole.setRoleId(dto.getRoleId());
				accountRole.setCreatedOn(new Date());
				accountRole.setCreatedBy(modifiedBy);
				accountRoleDao.insert(accountRole);
			}
		return result;
	}

	@Override
	public boolean delete(String userId) {
		boolean result = false;
			result = dao.deleteById(userId) > 0;
			if(result)
			{
				accountRoleDao.deleteAccountRoleByUserId(userId);
			}
		return result;
	}

	@Override
	public boolean saveAccount(AccountDto dto, String createdBy) {
		boolean result = false;
			Account account = new Account();
			account.setUserName(dto.getUserName());
			account.setPassword(MD5Utils.getMD516(dto.getPassword()));
			account.setUserId(SnowFlakeUtils.generator());
			account.setUserCode(dto.getUserCode());
			account.setUserImage(dto.getUserImage());
			account.setCreatedOn(new Date());
			account.setCreatedBy(createdBy);
			result = dao.insert(account) > 0;
			if(result)
			{
				AccountRole accountRole =new AccountRole();
				accountRole.setUserRoleId(SnowFlakeUtils.generator());
				accountRole.setUserId(account.getUserId());
				accountRole.setRoleId(dto.getRoleId());
				accountRole.setCreatedOn(new Date());
				accountRole.setCreatedBy(createdBy);
				accountRoleDao.insert(accountRole);
			}
		return result;
	}

	@Override
	public IPage<Account> queryListAccount(GeneralSearchDto<AccountSearchDto> dto) {

		IPage<Account> page = dao.selectPage(new Page<>(dto.getPageNo(), dto.getPageSize()),
				new QueryWrapper<Account>().
				like(StringUtils.isNotEmpty(dto.getConditions().getUserName()), "user_name", dto.getConditions().getUserName())
				.or().like(StringUtils.isNotEmpty(dto.getConditions().getUserCode()), "user_code", dto.getConditions().getUserCode())
				);

		return page;
	}

	/**    
	 * @description: 通过账号编码获取对应的账号信息
	 * @author: chenxc
	 * @date: 2020年4月19日 上午10:54:21
	 * @param userCode
	 * @return   
	 * @see com.cnfangmao.masterdata.service.AccountService#findByUserCode(java.lang.String)   
	 */
	@Override
	public Account findByUserCode(String userCode) {
		Account account = null;
			    account = dao.findByUserCode(userCode);
		return account;
	}

	@Override
	public boolean changePassword(ChangePasswordDto dto) {
		boolean result = false;
		Account account = dao.selectById(dto.getUserId());
		account.setPassword(MD5Utils.getMD516(dto.getNewPassword()));
		result = dao.updateById(account) > 0;
		return result;
	}

}
